﻿using PureMVC.Interfaces;
using Rail.Commons;
using Rail.Start.Viewer;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rail.Start.Viewer
{
    public class MessageCenterMediator : PureMVC.Patterns.Mediator, IMediator
    {
        private ToolStripStatusLabel m_StatusLabel;
        private ToolStripProgressBar m_ProgressBar;
        private volatile bool StopProgressBar = true;
        private RichTextBox m_ListBox;
        public new const string NAME = "MessageCenterMediator";

        public ToolStripStatusLabel StatusLabel { get => m_StatusLabel; set => m_StatusLabel = value; }
        public ToolStripProgressBar ProgressBar { get => m_ProgressBar; set => m_ProgressBar = value; }

        public delegate void FunctionEvent(string name);
        public event FunctionEvent FunctionEventHandler;

        public MessageCenterMediator(MesssagePanel viewComponent, ToolStripStatusLabel statusLabel, ToolStripProgressBar progressBar)
            : base(NAME, viewComponent)
        {
            m_ListBox = viewComponent.RichTextBox;
            m_StatusLabel = statusLabel;
            m_ProgressBar = progressBar;
            FunctionEventHandler += MessageCenterMediator_FunctionEventHandler;
        }
        public override IList<string> ListNotificationInterests()
        {
            IList<string> list = new List<string>();
            list.Add(Messages.SHOWERRORMESSAGE);
            list.Add(Messages.SHOWINFOMESSAGE);
            list.Add(Messages.SHOWWARNINGMESSAGE);
            list.Add(Messages.SHOWPROGRESS);
            list.Add(Messages.HIDEROGRESS);
            list.Add(Messages.SHOWTOOLSTRIP);
            list.Add(Messages.FixFunction);
            return list;
        }
        public override void HandleNotification(INotification note)
        {
            switch (note.Name)
            {
                case Messages.SHOWERRORMESSAGE:
                case Messages.SHOWINFOMESSAGE:
                case Messages.SHOWWARNINGMESSAGE:
                    SetMessage(note.Name, note.Body.ToString());
                    break;
                case Messages.HIDEROGRESS:
                    StopProgress();
                    break;
                case Messages.SHOWPROGRESS:
                    ShowProgress();
                    break;
                case Messages.SHOWTOOLSTRIP:
                    break;
                case Messages.FixFunction:
                    FunctionEventHandler?.Invoke(note.Body.ToString());
                    break;
            }
        }
        private void StopProgress()
        {
            ProgressBar.Visible = false;
            StopProgressBar = true;
        }
        private void ShowProgress()
        {
            ProgressBar.Visible = true;
            StopProgressBar = false;
            new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    System.Threading.Thread.Sleep(20);
                    if (this.StopProgressBar) break;
                    if (m_ListBox.IsDisposed) break;
                    this.m_ListBox.BeginInvoke(new Action(() =>
                    {
                        this.m_ProgressBar.Value = i;
                    }));
                    if (i == 99) i = 0;
                }
            }).Start();
        }
        private void SetMessage(string type, string msg)
        {
            if (m_ListBox.IsDisposed) return;
            m_ListBox.BeginInvoke(new Action(() =>
            {
                var msgWithTime = "[" + DateTime.Now.ToString("HH:mm:ss FFF") + "] " + msg;
                if (!m_ListBox.ReadOnly)
                    m_ListBox.ReadOnly = true;
                m_ListBox.Select(m_ListBox.Text.Length, 0);
                m_ListBox.Focus();
                m_ListBox.SelectionColor = System.Drawing.Color.FromName(GetColor(type));
                m_ListBox.AppendText(msgWithTime);
                m_StatusLabel.Text = msgWithTime;
                m_ListBox.AppendText(Environment.NewLine);
                m_ListBox.ScrollToCaret();
            }));
        }

        private string GetColor(string type)
        {
            if (type == Messages.SHOWERRORMESSAGE)
            {
                return "red";
            }
            else if (type == Messages.SHOWWARNINGMESSAGE)
            {
                return "yellow";
            }
            else
            {
                return "black";
            }
        }

        /// <summary>
        /// 系统设置功能事件
        /// </summary>
        /// <param name="name"></param>
        private void MessageCenterMediator_FunctionEventHandler(string name)
        {
            switch (name)
            {
                default:
                    break;
            }
        }
    }
}
